<?php

class Site_IndexController extends Ehl_Controller_Action
{

    public function indexAction()
    {
		$game = Administration_Model_Games::gameToDisplay();
		$this->view->game = $game;
		
		$news = new Administration_Model_News();
		$list = $news->lister("-creationDate");
		$this->view->listNews = $list;
    }

    public function panelAction()
    {
        $mail = new Jnd_Mail_Local("utf-8");
        $formLogin = new Ehl_Form_Login("Formulairejnd");
        $formNewMember = new Site_Form_NewMember("Formulairejnd");
        
        $request = $this->getRequest();
        if ($request->isPost()) {
            $datas = $request->getPost();
            if ($formNewMember->isValid($datas)) {
                $user = new Administration_Model_Users();
                unset($datas['confirmPassword']);
                unset($datas['submit']);
                $user->setOptions($datas);
                $user->sauver();
                $dataStorage = $user->_getMapper()->map($user->toArray());
                Zend_Auth::getInstance()->getStorage()->write($dataStorage);
                
                $this->view->clearPassword = $datas['password'];
                $this->view->username = $datas['username'];
                $html = $this->view->render('index/mails/newUser.phtml');
                
                $classMail = "Jnd_Mail_" . ucfirst(APPLICATION_ENV);
                //    			$mail = new $classMail("utf-8");
                $mail->addTo($datas['email']);
                $mail->setBodyHtml($html);
                $mail->setFrom("test@test.fr");
                $mail->setSujet("[" . $_SERVER['SERVER_NAME'] . "]" . __('Account creation'));
                $configMail = Jnd_App::getConfig("mail");
                $transport = new Zend_Mail_Transport_Smtp($configMail['hostname'], $configMail['config']);
                $mail->send($transport);
                
                $this->redirect("index");
            }
        }
        
        $this->view->formLoginMember = $formLogin;
        $this->view->formNemMember = $formNewMember;
    }

    public function loginAction()
    {
        $request = $this->getRequest();
        $form = new Ehl_Form_Login('Formulaire');
        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                $username = $request->getParam("username");
                $password = $request->getParam("password");
                
                // Configure une instance avec des paramètres de constructeur ...
                $authAdapter = new Zend_Auth_Adapter_DbTable(Jnd_App::getResource("db"), 'users', 'username', 'password');
                
                // Règle les valeurs d'entrées des crédits
                // (en général, à partir d'un formulaire d'enregistrement)
                $authAdapter->setIdentity($username)->setCredential(md5($password));
                
                // Réalise la requête d'authentification, et sauvegarde le résultat
                $result = $authAdapter->authenticate();
                
                if ($result->isValid()) {
                    
                    // stocke l'identité comme objet dans lequel seulement username et
                    // real_name sont retournés
                    $storage = Zend_Auth::getInstance()->getStorage();
                    $storage->write($authAdapter->getResultRowObject());
                    
                    // stocke l'identité comme objet dans lequel la colonne password
                    // a été omis
                    $storage->write($authAdapter->getResultRowObject(null, 'password'));
                    
                    $this->_redirect($this->view->smartUrl("index", "index", "default"));
                } else {

                }
                Zend_Debug::dump(Zend_Auth::getInstance()->getIdentity());
            } else {
                $this->view->formLogin = $form;
            }
        }
        Zend_Debug::dump($_POST);
    }
}
